﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Speedy
{
    public class Strings
    {
        public static string Right(string str, int Length)
        {
            if (Length < 0)
            {
                throw new ArgumentException("");
            }
            if ((Length == 0) || (str == null))
            {
                return "";
            }
            int length = str.Length;
            if (Length >= length)
            {
                return str;
            }
            return str.Substring(length - Length, Length);
        }

        public static string Left(string str, int Length)
        {
            if (Length < 0)
            {
                throw new ArgumentException("");
            }
            if ((Length == 0) || (str == null))
            {
                return "";
            }
            if (Length >= str.Length)
            {
                return str;
            }
            return str.Substring(0, Length);
        }

        public static int Asc(char String)
        {
            int num;
            int num2 = Convert.ToInt32(String);
            if (num2 < 0x80)
            {
                return num2;
            }
            try
            {
                byte[] buffer;
                Encoding fileIOEncoding = System.Text.Encoding.Default;
                char[] chars = new char[] { String };
                bool IsSingleByte = false;
                if (IsSingleByte)
                {
                    buffer = new byte[1];
                    int num3 = fileIOEncoding.GetBytes(chars, 0, 1, buffer, 0);
                    return buffer[0];
                }
                buffer = new byte[2];
                if (fileIOEncoding.GetBytes(chars, 0, 1, buffer, 0) == 1)
                {
                    return buffer[0];
                }
                if (BitConverter.IsLittleEndian)
                {
                    byte num4 = buffer[0];
                    buffer[0] = buffer[1];
                    buffer[1] = num4;
                }
                num = BitConverter.ToInt16(buffer, 0);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return num;
        }

        public static int Asc(string String)
        {
            if ((String == null) || (String.Length == 0))
            {
                throw new ArgumentException("");
            }
            char ch = String[0];
            return Asc(ch);
        }

        public static int Len(string Expression)
        {
            if (Expression == null)
            {
                return 0;
            }
            return Expression.Length;
        }

        public static string Hex(int Number)
        {
            return Number.ToString("X");
        }
        #region Mid
        public static string Mid(string str, int Start)
        {
            string str2;
            try
            {
                if (str == null)
                {
                    return null;
                }
                str2 = Mid(str, Start, str.Length);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str2;
        }
        public static string Mid(string str, int Start, int Length)
        {
            if (Start <= 0)
            {
                throw new ArgumentException("Argument_GTZero1");
            }
            if (Length < 0)
            {
                throw new ArgumentException("Argument_GEZero1");
            }
            if ((Length == 0) || (str == null))
            {
                return "";
            }
            int length = str.Length;
            if (Start > length)
            {
                return "";
            }
            if ((Start + Length) > length)
            {
                return str.Substring(Start - 1);
            }
            return str.Substring(Start - 1, Length);
        }
        #endregion
    }
}
